﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;

namespace Delphi.Mimir.Data.Entities.Final.Mappings {
    class ExchangeMap : EntityTypeConfiguration<Exchange> {
        public ExchangeMap() {
            /******************************************************************
            /* Table
            /*****************************************************************/
            ToTable("fin.eod_exchanges");

            /******************************************************************
            /* Primary Key
            /*****************************************************************/
            HasKey(e => e.ExchangeId);

            /******************************************************************
            /* Index
            /*****************************************************************/
            // IX_EXC_CD
            Property(e => e.Code)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_EXC_CD", 1) { IsUnique = true }));

            /******************************************************************
            /* Relationships
            /*****************************************************************/
            // Timezone
            HasOptional<TimeZone>(e => e.TimeZone)
                .WithMany()
                .HasForeignKey(e => e.TimeZoneId);

            // Country
            HasOptional<Country>(e => e.Country)
                .WithMany()
                .HasForeignKey(e => e.CountryId);

            // Currency
            HasOptional<Currency>(e => e.Currency)
                .WithMany()
                .HasForeignKey(e => e.CurrencyId);

            // Symbol
            HasMany<Symbol>(e => e.Symbols)
                .WithRequired(e => e.Exchange)
                .HasForeignKey(e => e.ExchangeId);

            // 1..n LoadControl
            HasMany<LoadControl>(a => a.LoadControls)
                .WithRequired(a => a.Exchange)
                .HasForeignKey(a => a.ExchangeId);

            /******************************************************************
            /* Properties
            /*****************************************************************/
            Property(e => e.ExchangeId)
                .IsRequired()
                .HasColumnName("exc_id");

            Property(e => e.Code)
                .IsRequired()
                .IsUnicode(false)
                .HasMaxLength(25)
                .HasColumnName("exc_cd");

            Property(e => e.Name)
                .IsOptional()
                .IsUnicode(false)
                .HasMaxLength(250)
                .HasColumnName("exc_desc");

            Ignore(e => e.Lable);

            Ignore(e => e.Lable2);

            Property(e => e.HasIntraDayProduct)
                .IsOptional()
                .HasColumnName("exc_intraday_fg");

            Property(e => e.IsActive)
                .IsOptional()
                .HasColumnName("exc_active_fg");

            Property(e => e.TimeZoneId)
                .IsOptional()
                .HasColumnName("exc_tiz_id");

            Property(e => e.CountryId)
                .IsOptional()
                .HasColumnName("exc_cnt_id");

            Property(e => e.CurrencyId)
                .IsOptional()
                .HasColumnName("exc_cur_id");

            Property(e => e.Suffix)
                .IsOptional()
                .HasMaxLength(25)
                .IsUnicode(false)
                .HasColumnName("exc_suffix_cd");

            Property(e => e.LocalOpeningHour)
                .IsOptional()
                .HasColumnName("exc_local_opening_hour");

            Property(e => e.LocalClosingHour)
                .IsRequired()
                .HasColumnName("exc_local_closing_hour");
        }
    }
}
